{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import itchat, time, requests, random\n",
    "from itchat.content import *\n",
    "\n",
    "replied = []\n",
    "\n",
    "@itchat.msg_register([TEXT])\n",
    "def text_reply(msg):\n",
    "    if '年' in msg['Text'] and msg['FromUserName'] not in replied:\n",
    "      sendGreeting(msg)\n",
    "\n",
    "@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])\n",
    "def other_reply(msg):\n",
    "  if msg['FromUserName'] not in replyed:\n",
    "    sendGreeting(msg)\n",
    "\n",
    "def sendGreeting(msg):\n",
    "  global replied\n",
    "  friend = itchat.search_friends(userName=msg['FromUserName'])\n",
    "  itchat.send((friend['RemarkName']+'，'+getRandomGreeting()), msg['FromUserName'])\n",
    "  replied.append(msg['FromUserName'])\n",
    "\n",
    "def getRandomGreeting():\n",
    "  response = requests.get(\"http://www.xjihe.com/api/life/greetings?festival=新年&page=10\", headers = {'apiKey':'sQS2ylErlfm9Ao2oNPqw6TqMYbJjbs4g'})\n",
    "  results = response.json()['result']\n",
    "  greeting = results[random.randrange(len(results))]['words']\n",
    "  return greeting\n",
    "\n",
    "itchat.auto_login(enableCmdQR=True,hotReload=True)\n",
    "itchat.run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
